<?php require '../public/system/headers/headers.php';?>
<?php echo $this->headLink()->appendStylesheet($this->websiteUrl . '/system/css/seotoaster.css'); ?>
<?php echo $this->headLink()->appendStylesheet($this->websiteUrl . 'system/js/thickbox/thickbox.css'); ?>
<?php $this->headScript()->appendFile($this->websiteUrl .'system/js/jquery/jquery.js'); ?>
<?php $this->headScript()->appendFile($this->websiteUrl . 'system/js/thickbox/thickbox.js'); ?>
<?php echo $this->headScript(); ?>

<div id="seotoaster" class="picasaService">
<div class="admineditwhat">{%Picasa service%}</div>
<div class="close_window" onclick="parent.tb_remove();"></div>
<?php  if ($this->error) : ?>
    <div class="errors" onclick="this.style.display='none';"><?php echo $this->errorMessage; ?></div>
<?php endif; ?>
<?php if (is_array($this->albums) && count($this->albums) > 0) : ?>
<!-- list albums -->
<h2 align="center">{%Your albums list%}</h2>
<div class="albums" style="width:570px;margin-left:20px;">
    <?php foreach ($this->albums as $key => $album) : ?>
    <div class="album">
    <?php if ($album->getMediaGroup()->getThumbnail() != null) :
        $mediaData = $album->getMediaGroup()->getThumbnail(); ?>
            <img src="<?php echo $mediaData[0]->getUrl(); ?>" />
        <?php else: ?>
            <img src="<?php echo $this->websiteUrl . 'system/images/no_preview.png'; ?>" />
        <?php endif; ?>
        <p align="left" class="name" title="<?php echo $album->title->text; ?>"><?php echo (strlen($album->title->text)<13?$album->title->text:substr($album->title->text, 0, 13).'...'); ?></p>
        <form action="<?php echo $this->websiteUrl . 'sys/backend_apiimages/picasa/'; ?>" method="post">
        <p align="right"><input type="hidden" name="name" value="<?php echo $album->title->text; ?>">
            <input type="hidden" name="request" value="album">
            <input type="submit" name="submit" class="formsubmit" value="{%show%}"></p>
        </form>
    </div>
    <?php endforeach; ?>
    <p style="clear:both;"></p>    
</div>
<!-- end list albums -->
<?php endif; ?>

<?php if (is_array($this->photos) && count($this->photos) > 0) : ?>
<!-- list photos -->
<script type="text/javascript">
var request = new Array();
function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    if(opacStart > opacEnd) {
        for(var i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(var i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
function addPhoto(form, websiteurl)
{
    var reg = /^([a-zA-Z0-9_\s-]{1,255})$/;
    var ltrim = /^\s+/g;
    var rtrim = /\s+$/g;
    var folder = '';
    var flag = false;
    var name = form.name.value;
    var photourl = form.photourl.value;
    if (document.formforselectfolder.newfolder.value != '' && document.formforselectfolder.newfolder.value != 'Create new folder') {
        folder = document.formforselectfolder.newfolder.value;
        flag = true;
    } else {
        var elf = document.formforselectfolder.folder;
        folder = elf.options[elf.selectedIndex].value;
    }
    folder = folder.replace(ltrim, '');
    folder = folder.replace(rtrim, '');
    name = name.replace(ltrim, '');
    name = name.replace(rtrim, '');
    photourl = photourl.replace(ltrim, '');
    photourl = photourl.replace(rtrim, '');
    if (!folder.match(reg) && flag == true) {
        document.formforselectfolder.newfolder.style.border ='1px solid red'; return;
    } else {
        document.formforselectfolder.newfolder.style.border ='1px solid #7C7C7C';
    }
    if (name == '') { alert('The name of photo can\'t be empty.'); return; }
    if (folder == '') { alert('Please select a folder.'); return; }
    if (photourl == '') { alert('The url of photo can\'t be empty.'); return; }
    var elf = document.formforselectfolder.folder;
    if (flag == true && elf.options[elf.selectedIndex].value != folder) {
        elf.options[elf.length] = new Option(folder, folder, false, true);
        document.formforselectfolder.newfolder.value = '';
    }
    form.submit.style.visibility = 'hidden';
    request.push(new Array(name,folder,photourl));
}
function queuePicasa()
{
    var url = '<?php echo $this->websiteUrl; ?>sys/backend_apiimages/importqueuepicasa/';
    if (request.length > 0 ) {
    var req;
    try { req = new XMLHttpRequest(); }
    catch (e) {
        try { req = new ActiveXObject('Msxml2.XMLHTTP'); }
        catch (e) {
            try { req = new ActiveXObject('Microsoft.XMLHTTP'); }
            catch (e) { return; }
        }
    }
    opacity('proccessdownload', 0, 100, 300);
    req.open('POST', url, true);
    req.onreadystatechange = function() {
        var el = document.getElementById('proccessdownload');
        if (req.readyState == 4) {
            if (req.status == 200) {
                document.getElementById('proccessdownload').style.border = '1px solid #c7c7c7';
            } else {
                document.getElementById('proccessdownload').style.border = '1px solid red';
            }
            opacity('proccessdownload', 100, 0, 300);
        }
    }
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.setRequestHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
    req.setRequestHeader('Pragma', 'no-cache');
    var query = '';
    var length = request.length;
    for(var i=0; i < length; i++) {
        if (length == i+1) {
            query += request[i].join('|||');
        } else {
            query += request[i].join('|||') + '~|~';
        }
    }    
    var data = 'data='+query;
    req.send(data);
    }
    request = new Array();
}
setInterval("queuePicasa();", 1000);
</script>
<div class="toolbar">
    <p><form action="javascript:;" name="formforselectfolder">
    <span>{%Import to:%} </span><select name="folder">
    <option value="" selected="selected">select folder</option>
    <?php if(!empty($this->listFolders) && is_array($this->listFolders)): ?>
        <?php foreach($this->listFolders as $folderName): ?>
            <option value="<?php echo $folderName; ?>"><?php echo $folderName; ?></option>
        <?php endforeach; ?>
    <?php endif; ?>
    </select> {%OR%} <input type="text" name="newfolder" value="Create new folder" onfocus="this.value='';"></form></p>
</div>
<div id="proccessdownload">Importing...</div><br />
<h3 align="center">{%Your photos%} <?php echo ($this->currentAlbum !== ''?' {%from the%} "' . (strlen($this->currentAlbum) < 25 ? $this->currentAlbum : substr($this->currentAlbum, 0, 25) . '...') .'" {%album%}.':''); ?></h3>
<div class="backtoalbums" title="back to albums" onclick="window.location.href = '<?php echo $this->websiteUrl . 'sys/backend_apiimages/picasa/'; ?>';"></div>
<div class="albums" style="height:370px;">
    <?php foreach ($this->photos as $photo) : ?>
    <div class="photo">
        <div class="image">
    <?php if ($photo[1] != '') : ?>
            <img src="<?php echo $photo[1]; ?>" title="<?php echo $photo[2]; ?>"/>
    <?php else: ?>
            <img src="<?php echo $this->websiteUrl . 'system/images/no_preview.png'; ?>" title="<?php echo $photo[2]; ?>"/>
    <?php endif; ?>
        </div>
        <form action="javascript:;" onsubmit="addPhoto(this, '<?php echo $this->websiteUrl; ?>');">
        <p align="center"><input type="hidden" name="name" value="<?php echo $photo[2]; ?>">
            <input type="hidden" name="photourl" value="<?php echo $photo[0]; ?>">
            <input type="submit" name="submit" class="formsubmit" value="{%import%}"></p>
        </form>
    </div>
    <?php endforeach; ?>
    <p style="clear:both;"></p>
</div>
<!-- list photos -->
<?php endif; ?>
<input type="hidden" id="hid-main-wsurl" name="hid-main-wsurl" value="<?php echo $this->websiteUrl; ?>" />
</div>